/**
 * @param {number[]} temperatures
 * @return {number[]}
 * @description 修言大佬 用栈解决
 */

var dailyTemperatures = function (temperatures) {
    // 缓存数组的长度
    const len = temperatures.length
    // 初始化栈
    const stack = []
    // 初始化结果数组
    const res = (new Array(len)).fill(0)
    for (let i = 0; i < len; i++) {
        // 若栈不为空 且打破了递减趋势
        while (stack.length && temperatures[i] > temperatures[stack[stack.length - 1]]) {
            // 将栈顶的元素对应的索引出栈
            const top = stack.pop()
            res[top] = i - top
        }
        stack.push(i)
    }
    return res
};

console.log(dailyTemperatures([73, 74, 75, 71, 69, 72, 76, 73]))
